﻿using System.Linq;
using System.Web.Mvc;
using ShoppingCart.Domain.Entities;
using ShoppingCart.Domain.Models;
using ShoppingCart.Business.Concrete;
using ShoppingCart.Business.Abstract;
using Ninject;

namespace ShoppingCart.WebUI.Controllers {

    public class ProductController : Controller {
        public int PageSize = 4; // We will change this later

        [Inject]
        public IProductService productService { get; set; }

        public ViewResult List(string category, int page = 1) {

            ProductsListViewModel viewModel = new ProductsListViewModel {
                Products = productService.Products
                    .Where(p => category == null ? true : p.Category == category)
                    .OrderBy(p => p.ProductID)
                    .Skip((page - 1) * PageSize)
                    .Take(PageSize),
                PagingInfo = new PagingInfo {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems =  category == null ?
                        productService.Products.Count() :
                        productService.Products.Where(e => e.Category == category).Count()
                },
                CurrentCategory = category
            };
            return View(viewModel);
        }

       
    }
}